//
//  KYNavigationController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYNavigationController.h"

@interface KYNavigationController ()

@end

@implementation KYNavigationController

- (instancetype)init
{
    if (self = [super init]) {
        
    }
    return self;
}

/**
 *  只创建一次
 */
+ (void)initialize
{
    // 1.设置导航栏主题
    [self setupNavBarTheme];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 重新拥有滑动出栈的功能
    self.interactivePopGestureRecognizer.delegate = nil;
}

/**
 *  设置导航栏主题
 */
+ (void)setupNavBarTheme
{
    // 1.获得bar对象
    UINavigationBar *navBar = [UINavigationBar appearance];
    
    // 2.设置导航栏颜色
    // 设置bar背景
    [navBar setBackgroundImage:[UIImage imageWithName:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    navBar.shadowImage = [[UIImage alloc] init];
    
    // 3.设置文字样式
    NSMutableDictionary *barAttrs = [NSMutableDictionary dictionary];
    barAttrs[NSForegroundColorAttributeName] = KYNavigationBarTitleColor;
    barAttrs[NSFontAttributeName] = KYNavigationBarTitleFont;
    [navBar setTitleTextAttributes:barAttrs];
    
    // 4.获得bauttonItem对象
    UIBarButtonItem *barItem = [UIBarButtonItem appearance];
    NSMutableDictionary *titleAttrs = [NSMutableDictionary dictionary];
    titleAttrs[NSForegroundColorAttributeName] = UIColorFromRGB(0x5e81e0);
    titleAttrs[NSFontAttributeName] = KYBarButtonTitleFont;
    [barItem setTitleTextAttributes:titleAttrs forState:UIControlStateNormal];
    [barItem setTitleTextAttributes:titleAttrs forState:UIControlStateHighlighted];
    
    NSMutableDictionary *disableAttrs = [NSMutableDictionary dictionary];
    disableAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    disableAttrs[NSFontAttributeName] = KYBarButtonTitleFont;
    [barItem setTitleTextAttributes:disableAttrs forState:UIControlStateDisabled];
}

/**
 *  重写导航栏控制push方法,重写此方法对控制器没有"侵入性"
 *
 *  @param viewController 拦截调用此方法的控制器，并对其进行设置
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count) {
        
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back_normal" higlightedImage:@"navigationbar_back_highlighted" target:self action:@selector(back)];
        
    }
    [super pushViewController:viewController animated:animated];
}

/**
 *  1.首先这个重写push方法 要调用super 第二要判断因为可能为nil
 *  2.第三进入第一个控制器是然后pop / pop to rootVC 没问题
 *  3.进入第三个是直接pop to rootVc 所以问题肯定出在这里 我们知道 控制器是在栈中进行 而栈的原则是先进先出 那么我
 *  们将栈顶控制器压出栈的时候要注意 看存在不存在 第二把方法改成 pop 到栈顶 而不要pop 到根控制器 这么解决！
 */

/**
 *  返回
 */
- (void)back
{
    [self popViewControllerAnimated:YES];
}

/**
 *  更多
 */
- (void)more
{
    [self popToRootViewControllerAnimated:YES];
}
@end
